package org.lora.core.context;

import java.util.List;
import java.util.Map;
import java.util.Properties;

import org.apache.log4j.Logger;
import org.lora.core.bean.ILoraBeanFactory;
import org.lora.core.bean.impl.LoraBeanFactoryImpl;
import org.lora.core.configxml.IConfigXmlExplain;
import org.lora.core.configxml.entity.ConfigXmlEntity;
import org.lora.core.configxml.impl.ConfigXmlExplainJDKImpl;
import org.lora.exception.LoraCoreException;
import org.lora.log4j.Log4jUtil;
import org.lora.mvc.IMvcService;
import org.lora.mvc.entity.ActionMethodEntity;
import org.lora.mvc.impl.MvcServiceImpl;
import org.lora.mvc.security.ISecurityService;
import org.lora.quartz.IQuartzService;
import org.lora.quartz.impl.QuartzServiceImpl;

/**
 * 上下文工厂<br>
 * 整个项目的生命周期内只有一个实例
 * 
 * @author 与龙共舞
 *
 */
/**
 * <p>
 * Title:WebContextFactory
 * </p>
 * <p>
 * Desription:上下文工厂<br>
 * 整个项目的生命周期内只有一个实例
 * </p>
 * Create Time:2016年7月14日 下午3:14:29
 * 
 * @author Bladnir@outlook.com
 *         <p>
 *         History:
 *         </p>
 *         2016年7月14日 Bladnir Create</br>
 */
public class WebContextFactory {

	private IConfigXmlExplain configXmlExplain = new ConfigXmlExplainJDKImpl();
	private IMvcService mvcService = new MvcServiceImpl();

	private Logger systemLog = Log4jUtil.getSystemLogger();

	/**
	 * xml文件的位置
	 */
	private String xmlFilePath;

	/**
	 * 构造方法
	 * 
	 * @param xmlFilePath
	 */
	public WebContextFactory(String xmlFilePath) {
		this.xmlFilePath = xmlFilePath;
	}

	/**
	 * 创建WebContext对象。 这个对象在整个项目运行周期内生效，所有向这个对象中注入的操作 都必须写在这个方法中！！！
	 */
	public void creatWebContext() {

		try {

			long time0 = System.currentTimeMillis();

			// Web上下文
			WebContext webContext = WebContext.getInstance();

			ConfigXmlEntity configXmlEntity = configXmlExplain.establishConfigXml(xmlFilePath);
			
			configXmlEntity.setQuartzDataSource("EnvironmentRunTime");
			webContext.setConfigXmlEntity(configXmlEntity);// loar.xml配置实体
			

			ILoraBeanFactory loraBeanFactory = new LoraBeanFactoryImpl(configXmlEntity);

			List<Object> actionObjectList = loraBeanFactory.assembleObject();
			
			Map<String, ActionMethodEntity> actionMap = mvcService.getActionMap(actionObjectList);

			webContext.addActionMap(actionMap);// actionMap

			long time4 = System.currentTimeMillis();

			systemLog.info("Lora WebContext Initialize Success ! Cost [" + (time4 - time0) + "] ms");
			
			
			long time5 = System.currentTimeMillis();
			ISecurityService securityService = webContext.getSecurityContext().getService();
			if(securityService != null){
				//初始化主体权限信息
				securityService.initPrincipalPermission();
				time5 = System.currentTimeMillis();
				systemLog.info("Principal Permission Initialize Success ! Cost [" + (time5 - time4) + "] ms");
			}
			
			//启动quartz框架
			Properties properties = configXmlEntity.getQuartzProperties();
			if(properties != null){
				IQuartzService quartzService = new QuartzServiceImpl();
				quartzService.initialize();
				webContext.setQuartzService(quartzService);
				long time6 = System.currentTimeMillis();
				systemLog.info("Quartz Initialize Success ! Cost [" + (time6 - time5) + "] ms");
			}


		} catch (LoraCoreException le) {
			systemLog.error(le.getMessage(), le);
		} catch (Exception e) {
			systemLog.error(e.getMessage(), e);
		}

	}

}
